package cn.codeforfun.user;

import cn.codeforfun.generator.model.User;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wangbin
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class UserVO extends User implements UserDetails {

    private List<RoleVO> roleVoList;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.getRoleVoList().stream()
                .flatMap(r -> r.getResourceList().stream())
                .map(r -> new SimpleGrantedAuthority(r.getResourceName()))
                .collect(Collectors.toList());
    }

    @Override
    public String getPassword() {
        return super.getUserPassword();
    }

    @Override
    public String getUsername() {
        return super.getUserName();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return super.getUserEnable();
    }
}
